from OWRpy import *
import os.path, redREnviron
import redRi18n
_ = redRi18n.get_(package = 'base')
import signals
import redRGUI


class gowdis(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''
        self.RFunctionParam_populate1=[]
        self.RFunctionParam_nrow='unknown (no data connected)'
        
        # R variables in the R session
        self.setRvariableNames(["gowdis"])
        
        #Inputs
        self.inputs.addInput('id0', _('R Dataframe Object'), signals.base.RDataFrame, self.processobject)
        
        #Outputs
        self.outputs.addOutput('id0', 'Dissimilarity', signals.base.RMatrix) # should output dissimilaroty signal
		
        #GUI
        self.RFunctionParam_method = redRGUI.base.comboBox(self.controlArea,  label = "Method: ",
        items = [('podani', 'Podani'),('metric', 'Metric'),('classic', 'Classic')],editable=False)
        
        self.RFunctionParam_options = redRGUI.base.checkBox(self.controlArea,  label = "Options", 
        buttons = [('na.rm','Ignore missing values in row or column standardizations.')],
        setChecked=None,orientation='vertical')
        
        self.RFunctionParam_weightValues = redRGUI.base.lineEdit(self.controlArea, label = "Weights")
        self.RFunctionParam_weightLabel = redRGUI.base.textEdit(self.controlArea, label="Weights intruction", html = ('Enter a comma separated list of weigths to tag the importance \nof the variables (leave the brackets). The needed number of weights is '+self.RFunctionParam_nrow+'.'))
        self.RFunctionParam_weightLabel.setDisabled(True)
        
        #Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)


    def processobject(self, data):
        if not self.require_librarys(["FD"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            self.RFunctionParam_nrow = int(self.R('nrow('+unicode(self.RFunctionParam_object)+')'))
            print 'NROW: ', self.RFunctionParam_nrow, type(self.RFunctionParam_nrow)
            for i in range(0,self.RFunctionParam_nrow+1):
                self.RFunctionParam_populate1.append(1)
            self.RFunctionParam_weightValues.setText(unicode(self.RFunctionParam_populate1))
            self.RFunctionParam_weightLabel.setText("Enter a comma separated list of weigths to tag the importance \n of the varaibles (leave the brackets). The needed number of weights is "+str(self.RFunctionParam_nrow)+".")
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''


    def commitFunction(self):
        
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data.')
            return

        if self.RFunctionParam_method.currentId() == '':
            self.status.setText('No method selected.')
            return
    
        self.RFunctionParam_weightValuesR=''
        for i in range(1,len(self.RFunctionParam_weightValues.text())-1):
            self.RFunctionParam_weightValuesR=self.RFunctionParam_weightValuesR+self.RFunctionParam_weightValues.text()[i]
        
        print 'self.RFunctionParam_weightValuesR =', self.RFunctionParam_weightValuesR

        self.R(self.Rvariables['gowdis']+'<-gowdis(x='+unicode(self.RFunctionParam_object)+
        ', w=c('+unicode(self.RFunctionParam_weightValuesR)+'))', processingNotice=True)      
        
        
        #self.R(self.Rvariables['dissimilarity']+'<-vegdist(x='+unicode(self.RFunctionParam_object)+', method="'+unicode(self.redRdissimilarityMethod.currentId())+'")')  
        
        self.newData = signals.base.RDataFrame(self, data = self.Rvariables["gowdis"], checkVal=False)
        self.rSend("id0", self.newData)
        
